/**
 * @author David Milne
 */

var koruGame;	
var koru ;

var collection = "korugame" ;
var collectionName = "Trec 2005 HARD Track Game Subset" ;
var serverPath = "www.greenstone.org/greenstone3" ;
var xmlHttp ;


function setup()
{
	try {
		logDebug("setting up") ;
	
 		//document.title = "Koru: " + collectionName ;	
 
 		// create Koru
  		this.koruGame = document.applyInherit(document.createElement("div"), new KoruGame()) ; 
		this.koruGame.init() ;
		this.koru = koruGame.koru ;

		// add koru to webpage
		var koruContainer = document.getElementById("koruContainer") ;
  		koruContainer.replaceChild(koruGame, koruContainer.firstChild) ;
		koruGame.resize() ;
	} catch (e) {
		logError(e) ;
	}
}


document.applyInherit = function(original, extended) {
 try { 
  // copy all methods and member variables  from interface to original
  // and return original

  for (method in extended)
   original[method] = extended[method];

  return original;
 } catch ( e ) { 
  output ( "util.applyInherit.error: " + e ); 
  throw(e) ;
 } 
}  


function getDocumentHeight() {
 return document.documentElement.clientHeight ;
}

function getDocumentWidth() {
 return document.documentElement.clientWidth ;
}

function percent(x) {
	return Math.round((x-0)*1000)/10; 
}

// this function determines whether the event is the equivalent of the microsoft // mouseleave or mouseenter events. 
function isMouseLeaveOrEnter(e, handler) { 

	if (e.type != 'mouseout' && e.type != 'mouseover') 
		return false; 
		
	var reltg = e.relatedTarget ? e.relatedTarget : e.type == 'mouseout' ? e.toElement : e.fromElement; 
	while (reltg && reltg != handler) 
		reltg = reltg.parentNode; 
		
	return (reltg != handler); 
}


function getUrlParam( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}



function arrayContains(array, item) {
	try {
		for (var i=0 ; i<array.length ; i++) {
			if (array[i] == item)
				return true ;
		}
		
		return false ;
	} catch (e) {
  		logError( "util.arrayContains.error: " + e ); 
  		throw (e) ;
 	}  
}

function replace(s, t, u) {
  /*
  **  Replace a token in a string
  **    s  string to be processed
  **    t  token to be found and removed
  **    u  token to be inserted
  **  returns new String
  */
  	var i = s.indexOf(t);
  	r = "";
  	if (i == -1) return s;
  		r += s.substring(0,i) + u;
  	if ( i + t.length < s.length)
    	r += replace(s.substring(i + t.length, s.length), t, u);
  	return r;
}

function trimDimension(dimension) {
	try {
   		return dimension.substring(0, dimension.length-2) - 0 ;
 	} catch (e) {
  		logError( "util.trimDimesion.error: " + e ); 
  		throw (e) ;
 	}  
}

function getAverageColor(color1, color2) {
 	var values1 = color1.substring(4, color1.length-1).split(",") ;
 	var values2 = color2.substring(4, color2.length-1).split(",") ;
 
 	var r = parseInt((parseInt(values1[0]) + parseInt(values2[0])) / 2) ;
 	var g = parseInt((parseInt(values1[1]) + parseInt(values2[1])) / 2) ;
 	var b = parseInt((parseInt(values1[2]) + parseInt(values2[2])) / 2) ;

 	return "rgb(" + r + "," + g + "," + b + ")" ;
}

//cookie management-------------------------------------------------------------------------

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
